﻿namespace NavigateTo2013
{
    using System.Collections.Generic;
    using System.Drawing;

    public class IconCache
    {
        static Dictionary<string, Icon> cache = new Dictionary<string, Icon>();

        public static Icon GetIcon(string filePath)
        {
            if (string.IsNullOrEmpty(filePath))
                return null;

            int dotIndex = filePath.LastIndexOf('.');
            if (dotIndex < 0)
                return null;

            string extension = filePath.Substring(dotIndex, filePath.Length - dotIndex).ToLower();
            if (!cache.ContainsKey(extension))
            {
                try
                {
                    cache[extension] = Icon.ExtractAssociatedIcon(filePath);
                }
                catch
                {
                    return null;
                }
            }

            return cache[extension];
        }
    }
}